document.getElementById('predict_type').addEventListener('click', function() {
    // 获取选中的视频的id
    let selectedVideos = Array.from(document.querySelectorAll('input[type="checkbox"]:checked:not(#flexCheckSelectAll)')).map(function(checkbox) {
        let row = checkbox.closest('tr');
        let videoId = checkbox.value;
        let title = row.querySelector('td:nth-child(4)').innerText;
        return {
            video_id: videoId,
            title: title
        };
    });

    if (selectedVideos.length === 0) {
        alert('请至少选择一个视频');
    }

    // 发送POST请求到指定接口
    axios.post('http://10.168.35.50:5001/predict', {
        video: selectedVideos
    }).then(function(response) {
        // 接收并处理返回的数据
        let predictions = response.data;
        predictions.forEach(function(prediction) {
            // 发送POST请求到Django后端
            console.log(prediction);
            axios.post('/update_video_category/', {
                video_id: prediction.video_id,
                category: prediction.prediction
            }).then(function(response) {
                // 处理响应
                if (response.data.status === 'success') {
                let checkbox = document.querySelector(`input[type="checkbox"]:checked:not(#flexCheckSelectAll)[value="${prediction.video_id}"]`);
                let row = checkbox.closest('tr');
                let categoryCell = row.querySelector('td:nth-child(5)');
                categoryCell.innerText = prediction.prediction;
                }
            });
        });
    }).catch(function(error) {
        // 处理错误
        console.error(error);
    });
});


let selectAllCheckbox = document.getElementById('flexCheckSelectAll');

selectAllCheckbox.addEventListener('change', function() {
    // 获取页面中所有的视频复选框
    let checkboxes = document.querySelectorAll('.form-check-input:not(#flexCheckSelectAll)');  // 不选择全选按钮本身

    // 根据全选按钮的状态检查或取消所有复选框
    checkboxes.forEach(function(checkbox) {
        checkbox.checked = selectAllCheckbox.checked;
    });
});


